# !/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2019/8/19 14:15
# @Author  : LiHaowen
# @Email   : lihaowen2017@163.com
from __future__ import unicode_literals

from django.utils.encoding import python_2_unicode_compatible
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.urls import reverse

# python2 可以向python3一样处理unicode字符
@python_2_unicode_compatible
class User(AbstractUser):
    # 自定义用户模型
    nickname = models.CharField(null=True, blank=True, max_length=255, verbose_name='昵称')
    job_title = models.CharField(max_length=50, null=True, blank=True, verbose_name='职称')
    introduction = models.TextField(blank=True, null=True, verbose_name="简介")
    picture = models.ImageField(upload_to='profile_pics/', null=True, blank=True, verbose_name="头像")
    location = models.CharField(max_length=50, null=True, blank=True, verbose_name="城市")
    mobile = models.CharField(null=True, blank=True, max_length=11, verbose_name="电话")
    personal_url = models.URLField(max_length=1000, null=True, blank=True, verbose_name="个人链接")
    weibo = models.URLField(max_length=1000, null=True, blank=True, verbose_name="微博链接")
    zhihu = models.URLField(max_length=1000, null=True, blank=True, verbose_name="知乎链接")
    github = models.URLField(max_length=1000, null=True, blank=True, verbose_name="GitHub链接")
    linkedin = models.URLField(max_length=1000, null=True, blank=True, verbose_name="LinkedIn链接")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now_add=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "用户"
        verbose_name_plural = verbose_name
        db_table = "tb_user"

    def __str__(self):
        return self.username

    def get_absolute_url(self):
        """返回用户详情页的url路径"""
        return reverse("users:detail", kwargs={"username": self.username})

    def get_profile_name(self):
        """返回用户昵称"""
        if self.nickname:
            return self.nickname
        return self.username
